/*
    Copyright (c) 2008-2009, Prashanta Shrestha All Rights Reserved.
    Available under GNU Lesser General Public License >= 3.0 as published by the Free Software Foundation.
    see <http://www.gnu.org/licenses/>
*/
/*-------------------------------------------------------------------------------------
  CLASS : qDojo.dijix.LoadingDialog
  ==================================
  - Loading dialog widget<b> 
  - When shown overlays the page with trans. div to prevent user interaction  
  - CSS- loadingDialog.css
  -------------------------------------------------------------------------------------*/
dojo.provide("qDojo.dijix.LoadingDialog");

dojo.require("dijit._Widget");
dojo.require("dijit._Templated");
dojo.require("qDojo.dijix.Rounded");
dojo.require("dojox.fx");
dojo.declare("qDojo.dijix.LoadingDialog", [dijit._Widget, dijit._Templated] , {
						
		templatePath	: dojo.moduleUrl("qDojo.dijix", "template/LoadingDialog.html"),
		
		duration: 600,		
		
		shown: false,	// check this before show/hide command
								
		postCreate: function() 
		{
			dojo.body().appendChild(this.domNode);
			this.inherited(arguments);			
			var s = this.domNode.style;
			s.visibility = "hidden";
			s.position = "absolute";
			s.display = "";
			s.top = "-9999px";
			var txt = "<div class=\"dijixLoadingDialogContent\"><span class=\"dijixLoadingDialogText\">LOADING</span></div>";
			var rounded = new qDojo.dijix.Rounded({style:"width:300px" , bgType:1 , content: txt})
					
			this.containerNode.appendChild(rounded.domNode);
											
			//<div dojoAttachPoint="titleBarText" class="dijixLoadingDialogText">LOADING</div>
			//<div dojoAttachPoint="titleBarImage" class="dijixLoadingDialogImage">&nbsp;</div> 		
		},
		_position: function()
		{
			// summary: position modal dialog in center of screen			
			if(dojo.hasClass(dojo.body(),"dojoMove")){ return; }
			var viewport = dijit.getViewport();
			var mb = dojo.marginBox(this.domNode);

			var style = this.domNode.style;
			style.left = Math.floor((viewport.l + (viewport.w - mb.w)/2)) + "px";
			style.top = Math.floor((viewport.t + (viewport.h - mb.h)/2)) + "px";
		},	
		_setup: function()
		{
			this._underlay = new dijit.DialogUnderlay({
				id: this.id+"_underlay" ,
				"class": dojo.map(this["class"].split(/\s/), function(s){ return s+"_underlay"; }).join(" ") 
			});
			
			var node = this.domNode;
			this._fadeIn = dojo.fx.combine(
				[dojo.fadeIn({
					node: node,
					duration: this.duration
				 }),
				 dojo.fadeIn({
					node: this._underlay.domNode,
					duration: this.duration,
					onBegin: dojo.hitch(this._underlay, "show")
				 })
				]
			);
			this._sizeTo = dojox.fx.sizeTo({
                // basic requirements:
                node: node, // or a domNode reference        
                width: 200, // measured in px
                height: 200 // measured in px
              
        	});
        	
			this._fadeOut = dojo.fx.combine(
				[dojo.fadeOut({
					node: node,
					duration: this.duration,
					onEnd: function(){
						node.style.visibility="hidden";
						node.style.top = "-9999px";
					}
				 }),
				 dojo.fadeOut({
					node: this._underlay.domNode,
					duration: this.duration,
					onEnd: dojo.hitch(this._underlay, "hide")
				 })
				]
			);
			
		},				
		show : function()
		{			
			if(!this._alreadyInitialized){
				this._setup();
				this._alreadyInitialized=true;
			}			
			dojo.style(this.domNode, "opacity", 0);
			this.domNode.style.visibility="";
			this.domNode.style.zIndex="1000";
			this._position();
			this._fadeIn.play();					
			this.shown = true;
		},
		hide :function()
		{
			this._fadeOut.play();			
			this.shown = false;	
/*			var s = this.domNode.style;
			s.visibility = "hidden";
			s.top = "-9999px";
			this._underlay.hide();*/
		}				
});


